package opera;

import Book.BookList;
import Book.book;

import java.util.Scanner;

/**
 * @Author: shark爱吃辣椒
 * @Date: 2022-19-14:38
 * @Description:
 */
public class Delete implements IOpera{

    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入要删除图书的名字：");
        Scanner sca = new Scanner(System.in);
        String name = sca.nextLine();
        int bookslength  = bookList.getBooksnum();
        int index = -1;
        for (int i = 0; i < bookslength; i++) {
            book book = bookList.getbook(i);
            if(book.getName().equals(name)){
               index= i;
               break;
            }
        }
       if(index != -1 ){
           for (int j = index; j < bookslength; j++) {
               book book = bookList.getbook(j+1);
               bookList.setBooks(j,book);
           }

           bookList.setBooksnum(bookslength-1);
           bookList.setBooks(bookslength-1,null);
           System.out.println("删除成功");
       }else{
           System.out.println("没有找到");
       }


    }
}
